package com.healthcloud.controller.modules.instrument;

import java.util.*;

import com.healthcloud.entity.MsgVo;
import com.healthcloud.entity.TestresultEntity;
import com.healthcloud.service.TestresultService;
import com.healthcloud.utils.DateUtils;
import com.healthcloud.utils.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.healthcloud.entity.MiInstrumentInstallEntity;
import com.healthcloud.service.MiInstrumentInstallService;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 仪器安装档案
 * 
 * @author yucl
 * @email yucailong1111@126.com
 * @date 2018-06-05 16:57:58
 */
@ApiIgnore
@Controller
@RequestMapping("miInstrumentinstall")
public class MiInstrumentInstallController {
	@Autowired
	private MiInstrumentInstallService miInstrumentInstallService;

	@Autowired
    private TestresultService testresultService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public String list(@RequestParam Map<String, Object> params){
		return "newframe/instrument/instrumentmgr";
	}

    /**
     * 查询列表数据（方法一：controller层处理日期）
     */
	@RequestMapping(value = "/search")
	@ResponseBody
    public MsgVo  searchMiMedicalinstrumentInstall(MiInstrumentInstallEntity bean, String number, @ModelAttribute("pageParam")PageParam pageParam){
        if(number != null && !"".equals(number) && number.matches("[0-9]{1,}")) {
            Calendar calendar = DateUtils.beforeNDays(DateUtils.Date2Calendar(new Date()),Integer.valueOf(number));
            String time = DateUtils.format(calendar,"yyyy-MM-dd HH:mm:ss");
            bean.setSearchTime(time);
        }
        List<MiInstrumentInstallEntity> list = miInstrumentInstallService.queryList(bean, pageParam);
        if(list != null) {
            int days;
            for(MiInstrumentInstallEntity entity :list) {
                if(entity.getLastTime() != null) {
                    days = (int) ((new Date().getTime() - entity.getLastTime().getTime()) / (1000 * 60 * 60 * 24));
                    if (days > 0) {
                        entity.setUseStatus("已有" + days + "天未使用");
                        if (days <= 3) {
                            entity.setColor("black");
                        } else if (days > 3 && days <= 7) {
                            entity.setColor("orange");
                        } else if (days > 7) {
                            entity.setColor("red");
                        }
                    } else {
                        List<TestresultEntity> testresultList = testresultService.queryBySN(entity.getMkMedicalInstrumenId());
                        int time = 0;
                        for(TestresultEntity testresultEntity : testresultList) {
                            days = (int)((new Date().getTime() - testresultEntity.getTestTime().getTime())/(1000*60*60*24));
                            if(days == 0) {
                                time ++ ;
                            }else {
                                break;
                            }
                        }
                        entity.setUseStatus("正常使用（" + time + "）");
                        entity.setColor("black");
                    }
                }else {
                    entity.setUseStatus("从未使用");
                    entity.setColor("red");

                }
            }
        }
        int count = miInstrumentInstallService.queryTotal(bean);
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }

/*
    */
/**
     * 查询列表数据（方法二：sql语句处理日期）
     *//*

    @RequestMapping(value = "/search")
    @ResponseBody
    public MsgVo  search(MiInstrumentInstallEntity bean, String number, @ModelAttribute("pageParam")PageParam pageParam){
        if(number != null && !"".equals(number) && number.matches("[0-9]{1,}")) {
            Calendar calendar = DateUtils.beforeNDays(DateUtils.Date2Calendar(new Date()),Integer.valueOf(number));
            String time = DateUtils.format(calendar,"yyyy-MM-dd HH:mm:ss");
            bean.setSearchTime(time);
        }
        List<MiInstrumentInstallEntity> list = miInstrumentInstallService.queryList(bean, pageParam);
        if(list != null) {
            int days;
            for(MiInstrumentInstallEntity entity :list) {
                if(entity.getLastTime() != null) {
                    days = (int) ((new Date().getTime() - entity.getLastTime().getTime()) / (1000 * 60 * 60 * 24));
                    if (days > 0) {
                        entity.setUseStatus("已有" + days + "天未使用");
                        if (days <= 3) {
                            entity.setColor("black");
                        } else if (days > 3 && days <= 7) {
                            entity.setColor("orange");
                        } else if (days > 7) {
                            entity.setColor("red");
                        }
                    } else {
                        List<TestresultEntity> testresultList = testresultService.queryBySN(entity.getMkMedicalInstrumenId());
                        int time = 0;
                        for(TestresultEntity testresultEntity : testresultList) {
                            days = (int)((new Date().getTime() - testresultEntity.getTestTime().getTime())/(1000*60*60*24));
                            if(days == 0) {
                                time ++ ;
                            }else {
                                break;
                            }
                        }
                        entity.setUseStatus("正常使用（" + time + "）");
                        entity.setColor("black");
                    }
                }else {
                    entity.setUseStatus("从未使用");
                    entity.setColor("red");

                }
            }
        }
        int count = miInstrumentInstallService.queryTotal(bean);
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }
*/

    /**
     * 跳转至smart仪器检测结果页面
     */
    @RequestMapping("/toShow")
    public String toShow(Model model, String mkMedicalInstrumenId) {
        model.addAttribute("mkMedicalInstrumenId",mkMedicalInstrumenId);
        return "newframe/instrument/testresult";
    }

    /**
     * 查询smart仪器检测结果
     */
    @RequestMapping(value = "/showResult")
    @ResponseBody
    public MsgVo showResult(String mkMedicalInstrumenId, Model model, @ModelAttribute("pageParam")PageParam pageParam) {

        List<TestresultEntity> list = testresultService.queryResultBySN(mkMedicalInstrumenId, pageParam);
        int total = testresultService.countResultBySN(mkMedicalInstrumenId);
        return new MsgVo(list,total,total,pageParam.getsEcho());
    }

}
